package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecParamService specParamService;

    private SpecGroupService specGroupService;


    /**
     *根据分类id查询规格组及组内参数
     * @param cid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>>listGroupById(@RequestParam("id")Long cid){
        return ResponseEntity.ok(this.specGroupService.listGroupById(cid));
    }

//
//    @GetMapping("/list")
//    public ResponseEntity<List<SpecParamDTO>>queryParamByCategoryId(@RequestParam("id") Long cid){
//        return ResponseEntity.ok(this.specParamService.queryParamByCategoryId(cid));
//    }

    /**
     *4-1 新增规格租
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void>addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        this.specGroupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.status(201).build();
    }

    /**
     *4-2 修改规格组
     * @param specGroupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void>updataSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        this.specGroupService.updataSpecGroup(specGroupDTO);
        return ResponseEntity.status(204).build();
    }
    /**
     * 4-3根据商品分类id，查询规格组的集合,查询成功，返回规格组的集合
     * @param cid
     * @return
     */

    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>>listSpecGroupByCategoryId(@RequestParam("id") Long cid){
        return ResponseEntity.ok(this.specGroupService.listSpecGroupByCategoryId(cid));
    }



    /**
     * 4-5新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void>addParams(@RequestBody SpecParamDTO specParamDTO){
        this.specParamService.addParams(specParamDTO);
        return ResponseEntity.status(201).build();
    }

    /**
     *4-6 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void>updataParams(@RequestBody SpecParamDTO specParamDTO) {
        this.specParamService.updataParams(specParamDTO);
        return ResponseEntity.status(204).build();
    }


    /**
     *  4-7根据条件查询规格参数集合
     * @param categoryId
     * @param groupId
     * @param searching
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>>querySpecParams(
                                                            @RequestParam(value = "categoryId", required = false) Long categoryId,
                                                          @RequestParam(value = "groupId", required = false) Long groupId,
                                                          @RequestParam(value = "searching", required = false) Boolean searching){


        return ResponseEntity.ok(this.specParamService.querySpecParams(categoryId,groupId,searching));
    }
}
